using Autofac;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace User.WebApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration, IHostEnvironment hostEnvironment)
        {
            Configuration = configuration;
            _hostEnvironment = hostEnvironment;
        }

        private readonly IHostEnvironment _hostEnvironment;
        public IConfiguration Configuration { get; }
        private IServiceCollection _services;

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            _services = services;
            services.AddServices(registion =>
            {
                //var policies = registion.GenerateDefaultRefitPolicies();
                //var authServeiceAddress = _environment.IsDevelopment() ? "http://localhost:5010" : "adnc.usr.webapi";
                //registion.AddRpcService<IAuthRpcService>(authServeiceAddress, policies);

                //var maintServiceAddress = _environment.IsDevelopment() ? "http://localhost:5020" : "adnc.maint.webapi";
                //registion.AddRpcService<IMaintRpcService>(maintServiceAddress, policies);

                //var whseServiceAddress = _environment.IsDevelopment() ? "http://localhost:8065" : "adnc.whse.webapi";
                //registion.AddRpcService<IWhseRpcService>(whseServiceAddress, policies);

                //registion.AddEventBusSubscribers<CapEventSubscriber>();
            });
        }

        public void ConfigureContainer(ContainerBuilder builder)
        {
            builder.RegisterModules(_services);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseMiddlewares();

            if (_hostEnvironment.IsProduction() || _hostEnvironment.IsStaging())
            {
                //app.RegisterToConsul();
            }
        }
    }
}
